package cn.b.sky.form.form.builder.impl;

import cn.b.sky.form.form.builder.BaseElementBuilder;
import cn.b.sky.form.model.BusinessField;
import cn.b.sky.form.model.ScreenField;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

public class SelectBuilder extends BaseElementBuilder {

  @Override
  public String buildHtmlElement(ScreenField sf) {

    BusinessField field = sf.getBf();
    StringBuilder sb = new StringBuilder();

    String value = field.getDefaultValue();
    sb.append("<select id=\"").append(field.getFieldLabel()).append("\"");
    if (sf.getReadonlyFlag() == 1) {
      sb.append(" disabled ");
    }
    if (!StringUtils.isBlank(field.getDbField()) && sf.getReadonlyFlag() != 1) {
      sb.append("name=\"").append(field.getDbField()).append("\"");
    }
    //输入框检验
    String validation = this.getValidationAttribute(field);
    if (!StringUtils.isBlank(validation)) {
      sb.append(" class=\"").append(validation).append("\"");
    }
    sb.append(">");
    List<Map<String, Object>> keyValues = this.getKeyValues(field);
    if (keyValues != null && !keyValues.isEmpty()) {
      sb.append("<option/>");
      for (Map<String, Object> map : keyValues) {
        sb.append("<option")
            .append(String.valueOf(map.get(SELECTVALUE)).equals(String.valueOf(value)) ? " selected " : "")
            .append(" value=\"").append(map.get(SELECTVALUE)).append("\" >").append(map.get(SHOWITEM))
            .append("</option>");
      }
    } 
    sb.append("</select>");
    return sb.toString();
  }

}
